#!/bin/python3

import carla
import random
import time

class AgentBase(object):
    def __init__(self, name: str,
                 agent_type: str,
                 world: carla.World,
                 spawn_transform: carla.Transform = None
                ):
        self.name = name
        self.agent_type = agent_type
        self.world = world
        self.spawn_transform = spawn_transform
        self.bps = self.world.get_blueprint_library().filter(agent_type)
    
    def random_generate(self, seed=None):
        if not None:
            seed = time.time()
        random.seed(seed)

        is_bicycle = True
        while is_bicycle:
            agent_bp = random.choice(self.bps)
            print("agent_bp.get_attribute('number_of_wheels'): {}", agent_bp.get_attribute('number_of_wheels').as_int())
            is_bicycle =  agent_bp.get_attribute('number_of_wheels').as_int() == 2
            time.sleep(0.1)
        print("agent bp id: {}", agent_bp.id)

        agent = None
        while not agent:
            self.spawn_transform = self._random_spawn_transform(seed)
            agent = self.world.try_spawn_actor(agent_bp, self.spawn_transform)
            time.sleep(0.1)

        return agent

    def _random_spawn_transform(self, seed=None):
        total_spawn_points = self.world.get_map().get_spawn_points()
        if not seed:
            seed = time.time()
        random.seed(seed)
        transform = random.choice(total_spawn_points)
        return transform